<template>
	<view class="recharge">
		<view class="main">
			<view class="view">
				充值
				<input v-model="data.num" type="number" :maxlength="data.numberMaxlength" @input="checkNumber"
					placeholder="请输入充值金额">
			</view>
		</view>
		<view class="bot" @click="submit">
			提交
		</view>
		<u-popup :round="20" :zIndex="9999" :show="data.show" @close="data.show = false">
			<view class="confirmOrderBottom">
				<view class="top">
					<text></text>
					支付
					<image @tap="data.show = false" src="/static/img/index/101.png" mode="widthFix"></image>
				</view>
				<view class="confirmOrderBottom_1">
					<view v-for="(item,index) in paymentChannels" :key="index" class=""
						@tap="data.payChannel = item.value">
						<view class="left">
							<image :src="'/static/img/index/'+item.icon+'.png'" mode="widthFix"></image>
							{{item.name}}
						</view>
						<image
							:src="data.payChannel == item.value?'/static/img/index/49.png':'/static/img/index/52.png'"
							mode="widthFix">
						</image>
					</view>
				</view>
				<view class="bot">
					<view class="left">

					</view>
					<view class="right" @tap="pay">
						立即支付
					</view>
				</view>
			</view>

		</u-popup>
		<SanshuiPaymentPassword v-if="data.show1" :onConfirm="data.onConfirm" :onCancel="()=>{data.show1 = false}" />
	</view>
</template>

<script lang="ts" setup>
	import { reactive } from 'vue';
	import { rechargeBond } from '@/utils/api/shop'
	import { paymentChannels } from '@/utils/index'
	import SanshuiPaymentPassword from '@/components/sanshui-payment-password/sanshui-payment-password.vue'
	import { payment } from '@/utils/index'
	const data = reactive<any>({
		num: 0,
		payChannel: paymentChannels[0].value,
		numberMaxlength: 10,
		onConfirm: () => { },
	})
	const pay = () => {
		data.show = false;
		if (data.payChannel == 'WALLET') {
			data.show1 = true;
			data.onConfirm = (e) => {
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				rechargeBond({ data: { rechargeAmount: data.num, payChannel: data.payChannel, payPwd: e } }).then((res) => {
					uni.hideLoading()
					payment(data.payChannel, res.data).then((res) => {
						setTimeout(() => {
							uni.navigateBack()
						}, 1000)
					})
				}).catch(() => {
					uni.hideLoading()
				})
			}
			return
		}
		uni.showLoading({
			title: '加载中...',
			mask: true
		})
		rechargeBond({ data: { rechargeAmount: data.num, payChannel: data.payChannel } }).then((res) => {
			uni.hideLoading()
			payment(data.payChannel, res.data).then((res) => {
				setTimeout(() => {
					uni.navigateBack()
				}, 1000)
			})
		}).catch(() => {
			uni.hideLoading()
		})
	}
	const submit = () => {
		if (!data.num) {
			uni.showToast({
				title: '请输入金额',
				icon: 'none'
			})
			return;
		}
		data.show = true;


	}

	const checkNumber = (e) => {
		let value = e.detail.value;
		let dot = value.indexOf('.'); //包含小数点
		let reg = /^[0-9]+$/; //正整数
		if (dot > -1) {
			data.numberMaxlength = dot + 3; //长度是小数点后两位
		}
		if (reg.test(value)) { //如果是正整数不包含小数点
			data.numberMaxlength = 10;
		}
	}
</script>

<style lang="scss">
	.confirmOrderBottom {
		padding: 24rpx 43rpx;

		.top {
			display: flex;
			align-items: center;
			justify-content: space-between;

			image {
				width: 30rpx;
			}

			text {
				width: 30rpx;
			}

			font-size: 32rpx;
		}

		.bot {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 30rpx;

			.left {
				display: flex;
				flex-direction: column;
				font-size: 24rpx;

				text {
					margin-top: 10rpx;
					color: #FF263F;
				}
			}

			.right {
				width: 190rpx;
				line-height: 80rpx;
				background: #FF263F;
				text-align: center;
				border-radius: 80rpx;
				color: #fff;

			}
		}
	}

	.confirmOrderBottom_1 {
		>view {
			justify-content: space-between;
			// padding: 37rpx 35rpx;
			background: #fff;
			display: flex;
			align-items: center;
			margin-top: 32rpx;

			.left {
				display: flex;
				align-items: center;

				image {
					width: 48rpx;
					margin-right: 25rpx;
				}

				font-size: 24rpx;
			}

			>image {
				width: 36rpx;
			}
		}
	}

	.recharge {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background: #F4F6FA;

		.main {
			flex: 1;
			padding-top: 30rpx;

			view {
				background: #fff;
				font-size: 32rpx;
				border-radius: 24rpx;

				input {
					flex: 1;
					margin-left: 20rpx;
				}

				margin: 0 auto;
				width: 702rpx;
				height: 96rpx;
				display: flex;
				align-items: center;
				padding: 0 32rpx;
			}
		}

		>.bot {
			width: 560rpx;
			background: #FF263F;
			line-height: 80rpx;
			font-size: 32rpx;
			color: #fff;
			margin: 0 auto;
			margin-bottom: 50rpx;
			border-radius: 80rpx;
			background: #FF263F;
			text-align: center;
		}
	}
</style>